/**   
 *@Title: DateUtil.java 
 *@Package com.org.framework.tools 
 *@Description: TODO(用一句话描述该文件做什么) 
 *@author 123774135@qq.com   
 *@date 2017年1月30日 下午8:35:19 
 *@version V1.0   
 */
package com.biconn.bigdata.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Minutes;
import org.joda.time.Years;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName: DateUtil
 * @author 123774135@qq.com
 * @date 2017年1月30日 下午8:35:19
 */
public class DateUtil {
	
	private static Logger logger = LoggerFactory.getLogger(DateUtil.class);

	/**
	 * yyyy-MM-dd HH:mm:ss
	 */
	public static String format1 = "yyyy-MM-dd HH:mm:ss";
	/**
	 * yyyy-MM-dd
	 */
	public static String format2 = "yyyy-MM-dd";
	/**
	 * yyyy/MM/dd
	 */
	public static String format3 = "yyyy/MM/dd";
	/**
	 * yyyyMMddHHmmss
	 */
	public static String format4 = "yyyyMMddHHmmss";
	/**
	 * yyyyMMdd
	 */
	public static String format5 = "yyyyMMdd";
	/**
	 * yyyyMM
	 */
	public static String format6 = "yyyyMM";
	/**
	 * yyyyMM
	 */
	public static String format7 = "yyyy";
	
	/**
	 * HHmmss
	 */
	public static String format_time1="HHmmss";
	/**
	 * HH:mm:ss
	 */
	public static String format_time2="HH:mm:ss";
	
	
	/**
	 * yyyyMMddHHmm
	 */
	public static String format_mm = "yyyyMMddHHmm";
	
	

	public static String now(String dateForomate) {
		SimpleDateFormat formate = new SimpleDateFormat(dateForomate);
		return formate.format(new Date());
	}

	/**
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static Date parseDate(String date, String pattern) {
		try {
			DateFormat df = new SimpleDateFormat(pattern);
			Date parse = df.parse(date);
			return parse;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 根据指定的规则返回字符串
	 * @param pattern
	 * @return
	 */
	public static String getDateString(String pattern) {
		try {
			DateFormat df = new SimpleDateFormat(pattern);
			String format = df.format(new Date());
			return format;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 比较日期大小
	 * 
	 * @param d1
	 * @param d2
	 * @return d1>d2 = 1 d1<d2 = -1 d1=d2 = 0
	 */
	public static int compare_date(Date d1, Date d2) {
		try {
			if (d1.getTime() > d2.getTime()) {
				return 1;
			} else if (d1.getTime() < d2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}
	public static Date toDate(String date,String format){
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		Date parse = null;
		try {
			parse = sdf.parse(date);
		} catch (ParseException e) {
			logger.error("日期解析错误:"+date+",format:"+format);
			e.printStackTrace();
		}
		return parse;
	}
	
	/**
	 * 获取date所在星期的星期一
	 * @param date
	 * @param format 返回的格式
	 * @return
	 */
	public static String getMonDay(Date date,String format){
		SimpleDateFormat sdf=new SimpleDateFormat(format,Locale.CHINA);
        Calendar c=Calendar.getInstance(Locale.CHINA);
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTimeInMillis(date.getTime());
        c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
//        System.out.println("周一时间:"+sdf.format(c.getTime()));
        return sdf.format(c.getTime());
	}
	/**
	 * 获取date所在星期的星期天
	 * @param date
	 * @param format 返回的格式
	 * @return
	 */
	public static String getSunDay(Date date,String format){
		SimpleDateFormat sdf=new SimpleDateFormat(format,Locale.CHINA);
		Calendar c=Calendar.getInstance(Locale.CHINA);
		c.setFirstDayOfWeek(Calendar.MONDAY);
		c.setTimeInMillis(date.getTime());
		c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
//		System.out.println("周天时间:"+sdf.format(c.getTime()));
		return sdf.format(c.getTime());
	}

	public static int compare_str_date(String date1, String date2, String format) {
        DateFormat df = new SimpleDateFormat(format);
        try {
            Date dt1 = df.parse(date1);
            Date dt2 = df.parse(date2);
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
	
	public static String getStringDate(Date sDate, String format) {
		SimpleDateFormat sdf=new SimpleDateFormat(format);  
		return sdf.format(sDate);
	}
	
	//根据当前日期获取本月最后一天
	public static String getLastDayOfMonth(String date, String format) {
		Date sDate = parseDate(date, format);
		Calendar cDay1 = Calendar.getInstance();
		cDay1.setTime(sDate);
		final int lastDay = cDay1.getActualMaximum(Calendar.DAY_OF_MONTH);
		Date lastDate = cDay1.getTime();
		lastDate.setDate(lastDay);
		return getStringDate(lastDate, format);
	}
	
	//根据当前日期获取本月第一天
	public static String getFirstDayOfMonth(String date, String format) {
		Date sDate = parseDate(date, format);
		Calendar cDay1 = Calendar.getInstance();
		cDay1.setTime(sDate);
		final int firstDay = cDay1.getActualMinimum(Calendar.DAY_OF_MONTH);
		Date lastDate = cDay1.getTime();
		lastDate.setDate(firstDay);
		return getStringDate(lastDate, format);
	} 
	
	public static String getPastDate(int past, String format) {  
	       Calendar calendar = Calendar.getInstance();  
	       calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);  
	       Date today = calendar.getTime();  
	       SimpleDateFormat date = new SimpleDateFormat(format);  
	       String result = date.format(today);  
	       return result;  
	}  
	
	
	/**
	 * 求当前日期和指定字符串日期的相差天数
	 * 
	 * @param date
	 *            指定字符串日期 如: "2009-10-12"
	 * @param dateForomate           
	 * @return 返回类型 long 当前日期和指定字符串日期的相差天数
	 * 
	 */
	public static int getTodayIntevalDays(String start,String end,String  dateForomate) {
		try {
			DateTimeFormatter formatter = DateTimeFormat.forPattern(dateForomate);
			DateTime  end_datetime = DateTime.parse(end, formatter);
			DateTime  start_datetime = DateTime.parse(start, formatter);
			int days = Days.daysBetween(end_datetime, start_datetime).getDays();
			return days;
		} catch (Exception ee) {
			return 0;
		}
	}
	
	/**
	 * 求当前日期和指定字符串日期的相差天数
	 * 
	 * @param date
	 *            指定字符串日期 如: "2009-10-12"
	 * @param dateForomate           
	 * @return 返回类型 long 当前日期和指定字符串日期的相差天数
	 * 
	 */
	public static int getyearIntevalYears(String start,String end,String  dateForomate) {
		try {
			DateTimeFormatter formatter = DateTimeFormat.forPattern(dateForomate);
			DateTime  end_datetime = DateTime.parse(end, formatter);
			DateTime  start_datetime = DateTime.parse(start, formatter);
			int years = Years.yearsBetween(end_datetime, start_datetime).getYears();
			return years;
		} catch (Exception ee) {
			return 0;
		}
	}
	
	/**
	 * 求两个时间之间相差多少小时
	 * @param date1 时间1
	 * @param date2 时间2
	 * @param dateForomate 时间格式
	 * @return
	 */
	public static double getTimeIntevalHour(String end,String start,String  dateForomate) {
		try {
			DateTimeFormatter formatter = DateTimeFormat.forPattern(dateForomate);
			DateTime  end_datetime = DateTime.parse(end, formatter);
			DateTime  start_datetime = DateTime.parse(start, formatter);
			double hour = Hours.hoursBetween(start_datetime,end_datetime).getHours() ;
			return hour;
		} catch (Exception ee) {
			return 0l;
		}
	}
	
	/**
	 * 求两个时间之间相差多少分钟
	 * @param date1 时间1
	 * @param date2 时间2
	 * @param dateForomate 时间格式
	 * @return
	 */
	public static double getTimeIntevalMinutes(String end,String start,String  dateForomate) {
		try {
			DateTimeFormatter formatter = DateTimeFormat.forPattern(dateForomate);
			DateTime  end_datetime = DateTime.parse(end, formatter);
			DateTime  start_datetime = DateTime.parse(start, formatter);
			double minutes = Minutes.minutesBetween(start_datetime,end_datetime).getMinutes();
			return minutes;
		} catch (Exception ee) {
			return 0l;
		}
	}
	
	
	
	public static Date getADayBefore(Date date,int period) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DATE,cal.get(Calendar.DATE)-period);  
		return cal.getTime();
	}
	public static int getBetweenDays(String start, String end,String dateForomate) {
		DateTimeFormatter formatter = DateTimeFormat.forPattern(dateForomate);
		DateTime  end_datetime = DateTime.parse(start, formatter);
		DateTime  start_datetime = DateTime.parse(end, formatter);
		int day = Days.daysBetween(start_datetime,end_datetime).getDays();
		return Math.abs(day);
	}
	
	public static void main(String[] args) {
		/*	System.out.println(getMonDay(new Date(), "yyyyMMdd"));
			System.out.println(getSunDay(new Date(), "yyyyMMdd"));
			System.out.println(compare_str_date("20170621","20170601", "yyyyMMdd"));*/
//				Date d = DateUtil.getADayBefore(new Date(),1);
//			System.out.println(DateUtil.getStringDate(DateUtil.getADayBefore(new Date(),1), DateUtil.format2));
		Date date = DateUtil.parseDate("2018-08-14T11:26:21.455494", "yyyy-MM-dd'T'HH:mm:ss.SSS");
		System.out.println(DateUtil.getStringDate(date, DateUtil.format1));
	}
}
